Android ViewGroup事件分发

上篇文章已经分析了AndroidTouch事件分发。如果没看的建议先看一下。Android View的Touch事件分发
接下来我们开始写几种场景,得出一个初步的执行顺序,然后我们按照这个顺序开始分析。


首先我们自定义一个ViewGroup和一个View,然后重写相关事件进行打印:

场景一:正常返回superTouchView设置clickonTouchListener事件(onTouch返回false)
TouchViewGroup.png

TouchView.png

布局.png

TouchView设置事件.png

这时候我们点击一下TouchView,触发事件:

点击一下.png

可以看到触发的DOWN MOVE UP事件顺序都为:
ViewGroup.dispatchTouchEvent -> ViewGroup.onInterceptTouchEvent -> View.dispatchTouchEvent -> View.onTouch -> View.onTouchEven
只是在UP事件的时候最后多了一个click事件。


场景二:在场景一的基础上取消TouchViewonClick事件

TouchView取消click事件.png

这时候发现除了,执行的顺序变为了:
ViewGroup.dispatchTouchEvent -> ViewGroup.onInterceptTouchEvent -> View.dispatchTouchEvent -> View.onTouch -> View.onTouchEven->ViewGroup.onTouchEven
并且只有DOWN事件,其他事件就没有了。


场景三:在场景二的基础上TouchViewGrouponInterceptTouchEvent里面返回true


这个时候就只有DOWN事件,并且顺序为:
ViewGroup.dispatchTouchEvent -> ViewGroup.onInterceptTouchEvent -> ViewGroup.onTouchEvent


接下来我们通过源码来分析:
首先从ViewGroupdispatchTouchEvent入手

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
 @Override
public boolean dispatchTouchEvent(MotionEvent ev) {
//...
boolean handled = false;
//...


//1.取消之前的手势
// Handle an initial down.
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Throw away all previous state when starting a new touch gesture.
// The framework may have dropped the up or cancel event for the previous gesture
// due to an app switch, ANR, or some other state change.
cancelAndClearTouchTargets(ev);
resetTouchState();
}

//2.判断是否拦截
// Check for interception.
final boolean intercepted;
if (actionMasked == MotionEvent.ACTION_DOWN
|| mFirstTouchTarget != null) { //DOWN
//父类是否拦截 getParent().requestDisallowInterceptTouchEvent();来改变值
final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
if (!disallowIntercept) {
intercepted = onInterceptTouchEvent(ev);
ev.setAction(action); // restore action in case it was changed
} else {
intercepted = false;
}
} else {
// There are no touch targets and this action is not an initial down
// so this view group continues to intercept touches.
intercepted = true;
}

//....
//3.0 如果是不取消不拦截为down,并且dispatchTransformedTouchEvent返回为true的时候会为 mFirstTouchTarget赋值
// Check for cancelation.
final boolean canceled = resetCancelNextUpFlag(this)
|| actionMasked == MotionEvent.ACTION_CANCEL;
// Update list of touch targets for pointer down, if needed.
final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
TouchTarget newTouchTarget = null;
boolean alreadyDispatchedToNewTouchTarget = false;
//3.1 如果不取消并且不拦截的情况下,
if (!canceled && !intercepted) {
if (actionMasked == MotionEvent.ACTION_DOWN
|| (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
|| actionMasked == MotionEvent.ACTION_HOVER_MOVE) {// 3.2 DOWN的时候
//...
if (newTouchTarget == null && childrenCount != 0) {
//...
final View[] children = mChildren;
for (int i = childrenCount - 1; i >= 0; i--) {//3.3 反序for循环,为了先拿到上层的view
//...
//3.4 拿到child
final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
//...
//3.5 根据child给newTouchTarget赋值 DOWN的时候因为 mFirstTouchTarget==null 所以进不去 返回的是null
newTouchTarget = getTouchTarget(child);
}
//...
//3.6. 执行操作 是执行自己的dispatchTouchEvent还是child的dispatchTouchEvent
if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) {

//...
//3.7 子View如果返回true添加一个newTouchTarget 并且为mFirstTouchTarget赋值
newTouchTarget = addTouchTarget(child, idBitsToAssign);
//....
}
}
}
}
//...
// Dispatch to touch targets.
if (mFirstTouchTarget == null) {//执行自身的dispatchTouchEvent
// No touch targets so treat this as an ordinary view.
handled = dispatchTransformedTouchEvent(ev, canceled, null,
TouchTarget.ALL_POINTER_IDS);
} else {// mFirstTouchTarget已经赋值
// Dispatch to touch targets, excluding the new touch target if we already
// dispatched to it. Cancel touch targets if necessary.
TouchTarget predecessor = null;
TouchTarget target = mFirstTouchTarget;
while (target != null) {
final TouchTarget next = target.next;
if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {//执行完3.7操作的
handled = true;
} else {
final boolean cancelChild = resetCancelNextUpFlag(target.child)
|| intercepted;
if (dispatchTransformedTouchEvent(ev, cancelChild,
target.child, target.pointerIdBits)) {
handled = true;
}
if (cancelChild) {
if (predecessor == null) {
mFirstTouchTarget = next;
} else {
predecessor.next = next;
}
target.recycle();
target = next;
continue;
}
}
predecessor = target;
target = next;
}
}



return handled;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Cancels and clears all touch targets.
*/
private void cancelAndClearTouchTargets(MotionEvent event) {
if (mFirstTouchTarget != null) {
boolean syntheticEvent = false;
if (event == null) {
final long now = SystemClock.uptimeMillis();
event = MotionEvent.obtain(now, now,
MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
syntheticEvent = true;
}

for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
resetCancelNextUpFlag(target.child);
dispatchTransformedTouchEvent(event, true, target.child, target.pointerIdBits);
}
clearTouchTargets();

if (syntheticEvent) {
event.recycle();
}
}
}

//清楚所有的TouchTarget
/**
* Clears all touch targets.
*/
private void clearTouchTargets() {
TouchTarget target = mFirstTouchTarget;
if (target != null) {
do {
TouchTarget next = target.next;
target.recycle();
target = next;
} while (target != null);
mFirstTouchTarget = null;
}
}


//根据childVie得到TouchTarget
/**
* Gets the touch target for specified child view.
* Returns null if not found.
*/
private TouchTarget getTouchTarget(@NonNull View child) {
// DOWN的时候因为 mFirstTouchTarget==null 所以进不去 返回的是null
for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) {
if (target.child == child) {
return target;
}
}
return null;
}




/**
* Transforms a motion event into the coordinate space of a particular child view,
* filters out irrelevant pointer ids, and overrides its action if necessary.
* If child is null, assumes the MotionEvent will be sent to this ViewGroup instead.
*/
private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
View child, int desiredPointerIdBits) {
//伪代码
final boolean handled;
if (child == null) {//执行View.dispatchTouchEvent 也就是自己的dispatchTouchEvent
handled = super.dispatchTouchEvent(event);
} else {//执行child的dispatchTouchEvent
handled = child.dispatchTouchEvent(event);
}
return handled;
}



//添加TouchTarget 并且给mFirstTouchTarget赋值
/**
* Adds a touch target for specified child to the beginning of the list.
* Assumes the target child is not already present.
*/
private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) {
final TouchTarget target = TouchTarget.obtain(child, pointerIdBits);
target.next = mFirstTouchTarget;
mFirstTouchTarget = target;
return target;
}
  1. DOWN的时候,从注释和方法名可以看出,会调用cancelAndClearTouchTargets,然后在调用clearTouchTargets使mFirstTouchTarget = null用来废弃上一次的触摸手势。
  2. 接着判断父类需不需要拦截,先通过(mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0来判断,在这里可以通过getParent().requestDisallowInterceptTouchEvent(boolean disallowIntercept)来改变值,如果上面为判断为false再通过onInterceptTouchEvent的返回值来确定,这个函数默认情况下返回false
  3. 检测是否为取消事件,如果不是取消、不拦截并且为 DOWN事件的时候,就会对childView一个反序的for循环来遍历,并且执行dispatchTransformedTouchEvent操作,这个操作用来执行dispatchTouchEvent,如果childViewnull的话将执行View.dispatchTouchEvent,也就是自己的dispatchTouchEvent,反之执行childViewdispatchTouchEvent,如果执行dispatchTransformedTouchEvent返回的值是true那么将会调用addTouchTarget()为这个childView生成一个TouchTarget并且执行mFirstTouchTarget = target将之赋值于mFirstTouchTarget ,然后跳出for循环遍历,这个mFirstTouchTarget是用于判断后续的事件move up等事件是否进行拦截触发函数
  4. 判断操作,首先判断mFirstTouchTarget是否为null,如果是DOWN事件,不拦截不取消并且dispatchTransformedTouchEvent返回了true,那么将会不进入这个判断,如果不是,那么将会在这执行自身的dispatchTouchEvent函数并且将返回值赋于handled返回。进入else语句,在里面将其mFirstTouchTarget进行next遍历,里面的if语句则是DOWN事件下的dispatchTransformedTouchEvent返回true的情况,直接将其赋值,然后返回,里面的else语句则是,调用dispatchTransformedTouchEvent,然后将其返回值返回。

到这里,ViewGroups事件分发源码的流程就分析了,我们根据这个来说说上面的场景。

场景一:我们在TouchViewGroupdispatchTouchEvent正常返回super,DOWN事件先触发TouchViewGroupdispatchTouchEvent,然后就执行onInterceptTouchEvent是否拦截,onInterceptTouchEvent返回的是super,也就是false,所以就会通过dispatchTransformedTouchEvent来执行TouchViewdispatchTouchEvent,后面就是ViewTouch事件分发了,View流程将会按照dispatchTouchEvent->onTouchListener - > onTouchEvent的顺序执行,因为设置了点击事件,所以在这里就返回了true,这个时候就会通过addTouchTarget()mFirstTouchTarget赋值,下面就直接返回了true。然后在MOVEUP事件的时候,也是首先执行dispatchTouchEvent,调用super然后调用onInterceptTouchEvent询问是否拦截,还是false,但是这里因为不是DOWN事件,所以就不会进入判断对其childView反遍历,因为在DOWN的时候mFirstTouchTarget赋值了,所以这时候进入第4步的else语句里面,这时候就对其遍历执行dispatchTransformedTouchEvent,也就是dispatchTouchEvent,然后将其返回。

场景2:我们取消了点击事件,那么在DOWN的时候就不会给mFirstTouchTarget赋值,这个时候将会进入第4步的if判断里面,直接调用dispatchTransformedTouchEvent,所以事件就不会有拦截,最终返回false,所以后续将不会接受到任何事件

场景3:我们在TouchViewGroup的时候是在onInterceptTouchEvent返回true,所以我们intercepted=true,这时候就不会给mFirstTouchTarget赋值,这个时候就调用自身的dispatchTransformedTouchEvent,同样的返回false,后续将不会接受到事件。

通过源码的角度我们也知道了为什么会这么执行,初步有点模糊,我们需要通过项目慢慢的来完善对它的认知。希望对大家有所帮助。

参考链接:
http://www.jianshu.com/p/98d1895c409d
http://www.jianshu.com/p/e99b5e8bd67b

-------------The End-------------